package com.ming.controller;

import cn.hutool.core.util.StrUtil;
import com.ming.common.CodeMsg;
import com.ming.common.realm.ActiveUser;
import com.ming.common.Constant;
import com.ming.common.Result;
import com.ming.common.validator.ValidatorUtil;
import com.ming.from.SysUserForm;
import com.ming.query.SysUserQuery;
import com.ming.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.locks.Lock;

/**
 * @Author:何元才
 * @Desc:
 */

@RestController
@RequestMapping("/sysuser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 处理登录请求
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("login.do")
    public Object login(String username, String password, HttpServletRequest request, Model model) {
        // 根据用户名和密码换取token
        // 因为数据库密码是加密的  所有MD5
        Md5Hash md5Hash = new Md5Hash(password, Constant.SALT, Constant.COUNT);
        System.out.println(md5Hash);
        UsernamePasswordToken token = new UsernamePasswordToken(username, md5Hash.toString());
        // 获取认证主体
        Subject subject = SecurityUtils.getSubject();
        // 认证
        subject.login(token);
        // 返回数据
        ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
        request.getSession().setAttribute( "user",activeUser.getSysUser() );
        request.getSession().setAttribute( "username",activeUser.getSysUser().getRealname() );
        return new Result();
    }

    /**
     * 处理退出请求
     *
     * @return
     */
    @RequestMapping("logout.do")
    public Object logout() {
        //获取认证主体
        Subject subject = SecurityUtils.getSubject();
        //校验是否已经认证
        if (subject.isAuthenticated()) {
            //退出
            subject.logout();
        }
        //返回数据
        return new Result();
    }

    /**
     * 用户菜单
     *
     * @return
     */
    @RequestMapping("userMenu.do")
    public Object menu() {
        Result rs = sysUserService.menu();
        return rs;
    }

    /**
     * 分页查询用户信息
     *
     * @param query
     * @return
     */
    @RequestMapping("page.do")
    public Object page(SysUserQuery query) {
        Result rs = sysUserService.queryPage(query);
        return rs;
    }

    /**
     * 处理新增的请求 新增元素
     *
     * @param form
     * @return
     */
    @RequestMapping("add.do")
    public Object add(SysUserForm form) {

        //进行数据格式校验
        ValidatorUtil.validator(form);
        Result rs = sysUserService.add(form);
        return rs;
    }


    /**
     * 根据id 重置密码
     *
     * @param id
     * @return
     */
    @RequestMapping("reset.do")
    public Object reset(Integer id) {
        //进行数据格式校验
        ValidatorUtil.validator(id);
        Result rs = sysUserService.resetPassword(id);
        return rs;
    }

    /**
     * 根据id删除 逻辑删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete.do")
    public Object deleted(Integer id) {
        //进行数据格式校验
        System.out.println("来了");
        //ValidatorUtil.validator(id);
        Result rs = sysUserService.deleted(id);
        return rs;
    }

    /**
     * 设置用户角色
     *
     * @param userId
     * @param roleIds
     * @return
     */
    @RequestMapping("setRole.do")
    public Object setRole(@RequestParam("userId") Integer userId, @RequestParam("roleId") List<Integer> roleIds) {
        Result rs = sysUserService.setUserRole(userId, roleIds);
        return rs;
    }

    /**
     * 修改密码
     *
     * @return
     */
    @RequestMapping("updatePassword.do")
    public Object updatePassword(String password,String newPassword ) {

        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        String loginPassword = activeUser.getSysUser().getLoginPassword();
        Md5Hash md5Hash = new Md5Hash(password,Constant.SALT,Constant.COUNT);
        if (!StrUtil.endWithIgnoreCase(loginPassword,md5Hash.toString())){
            return new Result(CodeMsg.USER_PASSWORD_ERROR);
        }
        Result rs = sysUserService.updatePassword(activeUser.getSysUser().getId(),newPassword);
        return rs;
    }




}
